# -*- coding: utf-8 -*-


import numpy as np


def FeatureList2Index(features_list,bag=[]):
    if len(bag) == 0:
        bag = set()
        for features in features_list:
            bag.update(features)
        bag = list(bag)

    indexs_list=[]
    for features in features_list:
        indexs=[]
        for f in features:
            if f not in bag:
                continue
            i = bag.index(f)
            indexs.append(i)
        indexs_list.append(indexs)
    return indexs_list


def FeatureList2OneHot(features_list,bag=[]):
    if len(bag) == 0:
        bag = set()
        for features in features_list:
            bag.update(features)
        bag = list(bag)

    onehot_array = np.zeros((len(features_list),len(bag)),int)

    for i,features in enumerate(features_list):

        for j,w in enumerate(bag):
            if w in features:
                onehot_array[i][j] = 1

    return onehot_array

    # onehot_list=[]
    # for features in features_list:
    #     onehot=[]
    #     exist = 0
    #     for w in bag:
    #         if w in features:
    #             exist = 1.
    #         else:
    #             exist = 0.
    #         onehot.append(exist)
    #     onehot_list.append(onehot)
    # return onehot_list



def FeatureList2SVMSparseData(features_list,bag=[]):
    if len(bag) == 0:
        bag = set()
        for features in features_list:
            bag.update(features)
        bag = list(bag)

    onehot_list=[]
    for features in features_list:
        onehot={}
        exist = 0
        """
        for i, w in enumerate(bag):
            if w in features:
                onehot[i+1] = 1.
            #else:
            #    onehot[i+1] = 0.
        """
        for f in features:
            if f not in bag:
                continue
            index = bag.index(f)
            onehot[index+1] = 1.

        onehot_list.append(onehot)
    return onehot_list


# normally we should make sure the label exist in bag
def Label2Index(Y,bag=[]):
    if len(bag) == 0:
        bag = list(set(Y))

    indexs = []
    for i in Y:
        index = bag.index(i) + 1
        indexs.append(index)
    return indexs


if __name__ == '__main__':
    features_list = [
        ['a','f'],
        ['b','a'],
        ['b']
    ]

    print(FeatureList2Index(features_list))
    # [[0, 2], [1, 0], [1]]

    features = [
        'a','b','c','a','b'
    ]

    bag = ['a','b','c']

    print(Label2Index(features,bag))
    #[0, 2, 1, 0, 2]


    dd = [
        ['aa', 'cc', 'dd'],
        ['aa', 'bb', 'dd'],
        ['aa', 'bb', 'cc', 'dd'],
        ['cc']
    ]

    print(FeatureList2OneHot(dd))
    #[[1.0, 1.0, 1.0, 0.0], [1.0, 0.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [0.0, 1.0, 0.0, 0.0]]

    dd = [
        ['aa', 'cc', 'dd'],
        ['aa', 'bb', 'dd'],
        ['aa', 'bb', 'cc', 'dd'],
        ['cc']
    ]

    _dict = ['bb','cc']

    print(FeatureList2OneHot(dd,_dict))
    #[[0.0, 1.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]]

    print(FeatureList2SVMSparseData(dd,_dict))
    #[{2: 1.0}, {1: 1.0}, {1: 1.0, 2: 1.0}, {2: 1.0}]
    pass